<template>
    <div class="admin-user-lists-box">
        <div class="lists">
            <item
                    v-for="(item, index) of lists"
                    border="1"
                    :key="index">
                <div class="items">
                    <div class="info">
                        <div class="room">
                            <span>&#xe604;</span>
                            用户：{{item.user_name}}
                        </div>
                        <div class="gold">
                            <span style="color: #FF8D24">&#xe934;</span>
                            {{item.gold.number ? item.gold.number : 0}}
                        </div>
                    </div>
                </div>
            </item>
        </div>
        <next-page
                :loading="loading"
                :more="full"
                :data="lists.length===0"
                @more="loadMore"></next-page>

    </div>
</template>

<script>
    import apiUrl from '@/api/apiUrl'
    import {Toast} from 'mint-ui'
    import nextPage from '../../../components/nextPage'
    import Item from '../../../components/item'

    export default {
        name: "index",
        data () {
            return {
                lists: [],
                loading: false,
                full: false,
                page: 1,
                row: 20,
                keywords: '',
                search: false,
                timer: null
            }
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                vm.setData()
            })
        },
        components:{
            nextPage,
            Item
        },
        mounted(){
            this.loadMore()
        },
        //离开当前页面后执行
        destroyed: function () {
            window.clearInterval(this.timer)
        },
        methods: {
            loadMore() {
                this.timer = window.setInterval(() => {
                    this.setData()
                }, 5000)
            },
            setData (page = 1, row = 1000, fun = null) {
                let params = {
                    params: {
                        page: page,
                        row: row,
                        room_id: this.$route.query.room_id
                    }
                }
                this.$http.get(apiUrl.room.onlineUser, params).then(res => {
                    if (res.data.code === 0) {
                        this.$set(this, 'lists', res.data.result)
                        if (fun !== null) {
                            fun(res.data.result)
                        }
                        if (res.data.result.length < row) {
                            this.full = true
                        }
                    } else {
                        Toast(res.data.msg)
                    }
                }).catch(err => {
                    console.log(err)
                })
            }
        }
    }
</script>

<style scoped lang="less">
    @import "../../../assets/css/bases";
    .items {
        .time {
            height: 25px;
            color: @gray;
        }
        .info {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }
</style>